package exam;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import bean.User;
import exam.port.UserPort;

/**
 * Servlet implementation class QueryAll
 */
@WebServlet("/queryall.do")
public class QueryAll extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryAll() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String classname = request.getParameter("classname");
		String grade = request.getParameter("grade");
		String begin = request.getParameter("begin");
		String end = request.getParameter("end");
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
		SqlSession session = sessionFactory.openSession(true);
		UserPort userPort = session.getMapper(UserPort.class);
		User user = new User();
		if (name.isEmpty() || name == null) {
			user.setName(null);
		}else{
			user.setName(name);
			request.setAttribute("name", name);
		}		
		if (classname.isEmpty() || classname == null) {
			user.setClassroom(null);
		}else{
			user.setClassroom(Integer.valueOf(classname));
			request.setAttribute("classname", classname);
		}
	if (grade.isEmpty() || grade ==null) {
			user.setGrade(null);
		} else {
			user.setGrade(Integer.valueOf(grade));
			System.out.println(grade);
			request.setAttribute("grade", grade);
		}
		if (end.isEmpty() || end == null || begin.isEmpty() || begin==null) {
			user.setEndtime(null);
			user.setTime(null);
		}else{
			SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date1;
			Timestamp ends = null;
			Date date;
			Timestamp begins = null;
			try {
				System.out.println(end);
				date1 = simpleDateFormat1.parse(end.toString());
				ends=new Timestamp(date1.getTime());
				date = simpleDateFormat1.parse(begin.toString());
				begins=new Timestamp(date.getTime());
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			user.setEndtime(ends);
			user.setTime(begins);
			request.setAttribute("ends", ends);
			request.setAttribute("begins", begins);
		}
		List<User> queryAll = userPort.queryAll(user);
		 List<User> queryClass = userPort.queryClass();
		 request.setAttribute("queryClass", queryClass);
		request.setAttribute("queryAll", queryAll);
		request.getRequestDispatcher("/qurey.ftl").forward(request, response);
		session.close();
	}

}
